<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/22
 * Time: 16:39
 */

namespace Admin\Controller;
use Think\Page;

class VipsController extends AdminController
{
    public function index()
    {
        $p = I('p',0);
        $page_size = 15;
        $Vips = M('Vips');
        $count = $Vips->count();
        $list = $Vips->page($p . ',' . $page_size)->order('end_time desc')->select();
        $page = new Page($count, $page_size);
        $this->assign('list', $list);
        $this->assign('_page', $page->show());
        $this->assign('meta_title','会员列表');
        $this->display();
    }


    public function show()
    {
        $uid = I('uid', '');
        $map['uid'] = $uid;
        if ($uid != '') {
            $info = M('Member')->where($map)->find();
            $viptime = M('Vips')->where($map)->find();
            $this->assign('info', $info);
            $this->assign('viptime', $viptime);
            $this->assign('meta_title', '会员资料');
            $this->display();
        } else {
            $this->error('查询失败!');
        }
    }

    public function rule(){
        $p = I('p',0);
        $page_size = 15;
        $VipType = M('VipType');
        $count = $VipType->count();
        $list = $VipType->page($p . ',' . $page_size)->select();
        $page = new Page($count, $page_size);
        $this->assign('list', $list);
        $this->assign('_page', $page->show());

        $this->assign('meta_title','会员规则表');
        $this->display();
    }

    public function edit()
    {
        $id = I('id', '');
        if ($id!=''){
            $info = M('VipType')->where('id='.$id)->find();
            $this->assign('info',$info);
            $this->assign('meta_title','会员规则修改');
        }else{
            $this->assign('meta_title','会员规则添加');
        }
        $this->display('edit');
    }


    public function update()
    {
        $id = I('id', '');
        $name = I('name', '');
        $days = I('days', 0);
        $amount = I('amount', 0);
        $send = I('send', 0);
        $buy = I('buy', 0);
        $description = I('description', '');

        $data['name'] = $name;
        $data['days'] = $days;
        $data['amount'] = $amount;
        $data['send'] = $send;
        $data['buy'] = $buy;
        $data['description'] = $description;
//        $data['update_time'] = time();

        $VipType = M('VipType');

        if (empty($id)) {
//            $data['create_time'] = time();
            $res = $VipType->data($data)->add();
            if ($res) {
                $this->success('添加成功!',U('rule'));
            } else {
                $this->error('添加失败!',U('rule'));
            }
        } else {
            $where['id'] = $id;
            $count = $VipType->where($where)->count();
            if ($count) {
                $res = $VipType->where($where)->data($data)->save();
                if ($res) {
                    $this->success('更新成功',U('rule'));
                } else {
                    $this->error('更新失败!',U('rule'));
                }
            } else {
                $this->error('更新失败!',U('rule'));
            }
        }
    }

    public function del()
    {
        $Vip = M("Vips");
        $Member = M("Member");
        if (IS_POST) {
            $uids = I('post.uid');
            if (is_array($uids)) {
                foreach ($uids as $uid) {
                    $Member->where("uid='$uid'")->setField("vip",0);
                    $Vip->where("uid='$uid'")->delete();
                }
            }
            $this->success("删除成功！");
        } else {
            $uid = I('get.uid');
            $Member->startTrans();
            $status1 = $Member->where("uid='$uid'")->setField("vip",0);
            $status2 = $Vip->where("uid='$uid'")->delete();
            if ($status1 && $status2) {
                $Member->commit();
                $this->success("删除成功！");
            } else {
                $Member->rollback();
                $this->error("删除失败！");
            }
        }
    }

}